﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LifeTree.Core.Configuration;
using LifeTree.Core.Utils;
using System.Data.Entity;

namespace LifeTree.EntityFrameworkProvider
{
    public class DatabaseFactory : IDatabaseFactory
    {
        private readonly string connectionString;
        private LifeTreeContext context;

        public DatabaseFactory(IConnectionString connectionString)
        {
            Guard.ThrowIfNull(connectionString, "connectionString");

            this.connectionString = connectionString.Value;
        }

        public LifeTreeContext Create()
        {
            if (context == null)
            {
                System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseIfModelChanges<LifeTreeContext>());
                context = new LifeTreeContext(connectionString);
            }

            return context;
        }

        public void Dispose()
        {

        }
    }
}
